package Servlet;

import Dao.*;
import bean.customer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(urlPatterns = "/CustomerMangerServlet")
public class CustomerServlet extends BaseServlet{
    private customerDao customerDao = new customerDaoIpl();

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取请求参数==封装为customer对象

        Integer pid = Integer.parseInt(req.getParameter("id"));

        customer newcustomer = new customer(pid,req.getParameter("username"),req.getParameter("phonenumber"),req.getParameter("password"));
        //调用updatacusotomer方法，修改图书

        customerDao.updateCustomer(newcustomer);

        //重定向回用户列表管理页面
        resp.sendRedirect("http://localhost:8080/Hospital_line_call_system_war/CustomerMangerServlet?action=list");
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-------add");
        customer newcustomer = new customer(null,req.getParameter("username"),req.getParameter("phonenumber"),req.getParameter("password"));
        customerDao.addCustomer(newcustomer);
        resp.sendRedirect("http://localhost:8080/Hospital_line_call_system_war/CustomerMangerServlet?action=list");

    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的参数id，图书编程
        Integer id = Integer.parseInt(req.getParameter("pid"));
        //2.调用customerDao.delete()删除图书
        customerDao.deleteCustomerBYId(id);

        //3.重定向回用户列表管理页面
        resp.sendRedirect("http://localhost:8080/Hospital_line_call_system_war/CustomerMangerServlet?action=list");


    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        //1.通过customers查询全部用户
        List<customer> customers = customerDao.queryCustomers();
        //2.把全部用户保存到request域中
        req.setAttribute("customers",customers);
        //3.请求转发到customer_manager.jsp页面
        req.getRequestDispatcher("/customer_manager.jsp").forward(req,resp);
    }


    protected void getCustomer(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的用户电话号码

        System.out.println("我又来了老铁");

        Integer id = Integer.parseInt(req.getParameter("pid"));

        System.out.println(id);

        //2.//调用方法查询图书
        customer customer = customerDao.queryCustomerById(id);

        //3.保存用户数据到request域中
        req.setAttribute("customer",customer);

        //请求转发到edit_customer中
        req.getRequestDispatcher("/manager/Updatecustomer.jsp").forward(req,resp);
    }
}

